package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageDto;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Productitem;
import com.quickcancellation.crm.pojo.vo.DicKeyValVo;
import com.quickcancellation.crm.service.IDictionaryService;
import com.quickcancellation.crm.service.IProductitemService;
import com.quickcancellation.crm.utils.PageUtils;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-04
 */
@Controller
@RequestMapping("/productItem")
@CrossOrigin
@RestController
@SaCheckLogin
@Api(tags = "品规管理")
@SaCheckRole(value = {"productItem1", "productItem2", "productItem3"}, mode = SaMode.OR)
public class ProductitemController {
    private final static Logger logger = LoggerFactory.getLogger(ProductitemController.class);

    @Resource
    private IProductitemService productitemService;

    @Resource
    private IDictionaryService dictionaryService;

    /**
     * 分页查询列表
     *
     * @param params
     * @return
     */
    @PostMapping("/productItem1/list")
    @ApiOperation("品类分页查询列表")
    @LogAspect
    @SaCheckRole(value = {"productItem1", "productItem2"}, mode = SaMode.OR)
    public ResponseDto list(@RequestBody PageParamsDto<Productitem> params) {

        Productitem productItem = params.getParams();

        QueryWrapper<Productitem> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("itemType", 1);

        if (productItem != null) {

            if (StringUtils.hasLength(productItem.getItemName())) {
                queryWrapper.like("itemName", productItem.getItemName());
            }
            if (StringUtils.hasLength(productItem.getItemCode())) {
                queryWrapper.like("itemCode", productItem.getItemCode());
            }

            if (!"".equals(productItem.getStatus())) {
                queryWrapper.eq("status", productItem.getStatus());
            }
        }


        Page<Productitem> page = new Page<>(params.getPage(), params.getLimit());

        Page<Productitem> resultPage = productitemService.page(page, queryWrapper);

        return ResponseUtil.success(resultPage.getTotal(), resultPage.getRecords());
    }


    @PostMapping("/productItem2/list")
    @ApiOperation("品项分页查询列表")
    @LogAspect
    @SaCheckRole(value = {"productItem2", "productItem3"}, mode = SaMode.OR)
    public ResponseDto productItem2list(@RequestBody PageParamsDto<Productitem> params) {

        PageDto<Productitem> pageDto = PageUtils.getPageParams(params);

        List<Map<String, Object>> resultList = productitemService.getProductItem2List(pageDto);

        Long resultCount = productitemService.getProductItem2ListCount(pageDto);

        return ResponseUtil.success(resultCount, resultList);
    }

    @PostMapping("/productItem3/list")
    @ApiOperation("品项分页查询列表")
    @LogAspect
    @SaCheckRole(value = "productItem3")
    public ResponseDto productItem3list(@RequestBody PageParamsDto<Productitem> params) {

        PageDto<Productitem> pageDto = PageUtils.getPageParams(params);

        List<Map<String, Object>> resultList = productitemService.getProductItem3List(pageDto);

        Long resultCount = productitemService.getProductItem3ListCount(pageDto);

        return ResponseUtil.success(resultCount, resultList);
    }


    @PostMapping("/save")
    @ApiOperation("保存")
    @LogAspect
    public ResponseDto save(@RequestBody Productitem productItem) {

        if (!StringUtils.hasLength(productItem.getItemName())) {
            return ResponseUtil.fail("名称不能为空!");
        }
        if (!StringUtils.hasLength(productItem.getItemCode())) {
            return ResponseUtil.fail("编码不能为空!");
        }

        // 判断重复
        if (productitemService.list(new QueryWrapper<Productitem>().eq("itemName", productItem.getItemName())).size() > 0) {

            return ResponseUtil.fail("名称已存在");

        }

        if (productitemService.list(new QueryWrapper<Productitem>().eq("itemCode", productItem.getItemCode())).size() > 0) {

            return ResponseUtil.fail("编码已存在");

        }

        // 如果是根节点
        if (productItem.getItemType() == 1) {
            productItem.setPClassGuid("00000000-0000-0000-0000-000000000000");
            productItem.setCoefficient(null);
        }
        // 设置新clasGuid主键
        productItem.setClassGuid(UUID.randomUUID().toString());

        boolean result = productitemService.save(productItem);

        return ResponseUtil.ok(result);

    }


    @PostMapping("/update")
    @ApiOperation("更新")
    @LogAspect
    public ResponseDto save(@RequestBody Map<String, Object> params) {

        String status = MapUtils.getString(params, "status");

        if (!("1".equals(status) || status.equals("0"))) {
            return ResponseUtil.fail("状态错误");
        }

        Long id = MapUtils.getLong(params, "id");
        Double coefficient = MapUtils.getDouble(params, "coefficient");
        String isprodcut = MapUtils.getString(params, "isproduct");
        String pClassGuid = MapUtils.getString(params, "pClassGuid");

        Productitem productitem = new Productitem();
        if (null != coefficient) {
            productitem.setCoefficient(BigDecimal.valueOf(coefficient));

        }

        productitem.setId(id);
        productitem.setStatus(status);
        productitem.setIsproduct(isprodcut);
        productitem.setPClassGuid(pClassGuid);

        boolean result = productitemService.updateById(productitem);


        return ResponseUtil.ok(result);

    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    @LogAspect
    public ResponseDto delete(@RequestBody Map<String, Object> params) {

        Integer id = MapUtils.getInteger(params, "id");

        boolean result = productitemService.removeById(id);

        return ResponseUtil.ok(result);

    }


    @PostMapping("/getProductTypeList")
    @ApiOperation("获取物料字典值")
    @LogAspect
    public ResponseDto getProductTypeList() {

        List<DicKeyValVo> list = dictionaryService.getDicKeyAndValByModel("品规品项类型");

        return ResponseUtil.success(list);
    }
}
